/**
 * @Author: Bobby
 * @Date: 2022-03-09 14:38:58
 * @LastEditTime: 2022-03-18 17:43:10
 * @LastEditors: Bobby
 * @Description:
 * @FilePath: \wms-v2\src\ve\eventTask\actions\layer\closeLayer.js
 * @CIMC-V2
 */
import { findPrevParent } from '@ve/utils/utils'
//必须带done回调 执行完成调用
export default function closeLayer({ vm, action, done }) {
  // 这里是array 但是是单选 就默认取0
  if (action.extension.layer.value.length > 0) {
    let layerId = action.extension.layer.value[0]
    let getNowPageData = vm.$store.getters['preview/getNowPageData']
    // 查找要关闭的页面的信息
    let closeLayerInfo = {}
    const findLayerInfo = (list) => {
      list.forEach((i) => {
        if (i._id === layerId) closeLayerInfo = i
        i.list && findLayerInfo(i.list)
      })
    }
    findLayerInfo([getNowPageData])
    console.log('当前页面组件信息', closeLayerInfo)

    //查找图层id的父级
    let parentObj = findPrevParent([getNowPageData], layerId, '_id', 'list')

    const changeParent = (parentObj) => {
      parentObj.hasSubPageShow = false
      // parentObj.data = {}
      parentObj.subPage.info.data && (parentObj.subPage.info.data.requestData = {})
      parentObj.subPage.info = {}
    }
    if (parentObj) {
      changeParent(parentObj)
    }

    const changeSplitModule = (splitId) => {
      const splitModule = vm.vuiComponents[splitId] && vm.vuiComponents[splitId].self
      if (splitModule) {
        splitModule.info.showCustomPage = false
      }
    }
    if (closeLayerInfo.isSplitLayer) {
      // 如果是分割组件下打开的图层 则需要修改分割组件的一些状态
      changeSplitModule(closeLayerInfo.splitModuleInfo.splitId)
    } else {
      // 正常图层关闭则需要同时关闭分割组件下打开的图层
      const closeSplitLayer = (list, parent) => {
        list.forEach((i) => {
          if (i.isSplitLayer) {
            changeParent(parent)
            changeSplitModule(i.splitModuleInfo.splitId)
          }
        })
      }
      closeSplitLayer(closeLayerInfo.list || [], closeLayerInfo)
    }
  }
  done(null, {})
}
